rm(list = ls())

### environment
library(pyMTurkR)
Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
AccountBalance()
# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### qualification for word intrusion (WI)
TestQuestions <- paste0(readLines("WIQuestion.xml", warn = FALSE), collapse = "")
TestKey <- paste0(readLines("WIAnswer.xml", warn = FALSE), collapse = "")
WIQual <- CreateQualificationType(name = "word intrusion qualification", # name of the qual
                                  description = 'Qualification for "word intrusion" tasks. For each HIT, you will see FIVE words. Four of them will be related to each other, but one word will be out of place. Your job is to pick up the one word that does NOT belong with the others.',
                                  status = "Active", # allows qual to remain active for users
                                  test = TestQuestions, # pass questions for qual test
                                  test.duration = 60 * 60, # test duration, in seconds
                                  retry.delay = NULL, # how long until worker can retry test; NULL means never
                                  answerkey = TestKey) # answer key for your qual test

### qualification for top 8 word set intrusion (T8WSI)
TestQuestions <- paste0(readLines("T8WSIQuestion.xml", warn = FALSE), collapse = "")
TestKey <- paste0(readLines("T8WSIAnswer.xml", warn = FALSE), collapse = "")
T8WSIQual <- CreateQualificationType(name = "top eight word set intrusion qualification", # name of the qual
                                     description = 'Qualification for "top eight word set intrusion" tasks. For each HIT, you will see ONE short passage and four word sets. Three of the word sets will be related to the passage, but the other one will be out of place. Your job is to pick up the one word set that seems LEAST related to the passage.', # description of the qual
                                     status = "Active", # allows qual to remain active for users
                                     test = TestQuestions, # pass questions for qual test
                                     test.duration = 60 * 60, # test duration, in seconds
                                     retry.delay = NULL, # how long until worker can retry test; NULL means never
                                     answerkey = TestKey)

### qualification for random 4 word set intrusion (R4WSI)
TestQuestions <- paste0(readLines("R4WSIQuestion.xml", warn = FALSE), collapse = "")
TestKey <- paste0(readLines("R4WSIAnswer.xml", warn = FALSE), collapse = "")
R4WSIQual <- CreateQualificationType(name = "random four word set intrusion qualification", # name of the qual
                                     description = 'Qualification for "random four word set intrusion" tasks. For each HIT, you will see four word sets. Three of the word sets will be related to one another, but the other one will be out of place. Your job is to pick up the one word set that seems LEAST related to the others.', # description of the qual
                                     status = "Active", # allows qual to remain active for users
                                     test = TestQuestions, # pass questions for qual test
                                     test.duration = 60 * 60, # test duration, in seconds
                                     retry.delay = NULL, # how long until worker can retry test; NULL means never
                                     answerkey = TestKey) # answer key for your qual test

### qualification for label intrusion (LI)
TestQuestions <- paste0(readLines("LIQuestion.xml", warn = FALSE), collapse = "")
TestKey <- paste0(readLines("LIAnswer.xml", warn = FALSE), collapse = "")
LIQual <- CreateQualificationType(name = "label intrusion qualification", # name of the qual
                                  description = 'Qualification for "label intrusion" tasks.', # description of the qual
                                  status = "Active", # allows qual to remain active for users
                                  test = TestQuestions, # pass questions for qual test
                                  test.duration = 60 * 60, # test duration, in seconds
                                  retry.delay = NULL, # how long until worker can retry test; NULL means never
                                  answerkey = TestKey) # answer key for your qual test

### qualification for optimal label (OL)
TestQuestions <- paste0(readLines("OLQuestion.xml", warn = FALSE), collapse = "")
TestKey <- paste0(readLines("OLAnswer.xml", warn = FALSE), collapse = "")
OLQual <- CreateQualificationType(name = "optimal label qualification", # name of the qual
                                  description = 'Qualification for "optimal label" tasks.',
                                  status = "Active", # allows qual to remain active for users
                                  test = TestQuestions, # pass questions for qual test
                                  test.duration = 60 * 60, # test duration, in seconds
                                  retry.delay = NULL, # how long until worker can retry test; NULL means never
                                  answerkey = TestKey) # answer key for your qual test

### if you ever need a qualification without test
# QualificationWithoutTest <- CreateQualificationType(name = "", # name of the qual
#                                                     description = '', # description of the qual
#                                                     status = "Active") # allows qual to remain active for users

# save the qual objects once created
# save.image('Qualifications.RData') # you may need to set file path

# to load qual objects as needed
load('Qualifications.RData') # you may need to set file path

# to update a qualification
# in this example, I update the WI qual test
WIQual <- UpdateQualificationType(qual = WIQual$QualificationTypeId, # keep same qual id
                                  description = WIQual$Description, # keep same qual description
                                  status = 'Active', # manually set status
                                  test = TestQuestions, # update test
                                  test.duration = 60 * 60, # manually set test duration
                                  retry.delay = NULL, # manually set retry delay
                                  answerkey = TestKey) # update answer key

# to delete qualifications
# DisposeQualificationType(QualificationWithTest$QualificationTypeId)
